package com.woniu.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;


@Component
@Configuration
public class RabbitMQConfig {



    /*
    延迟队列
     */
    @Bean
    public Queue orderNumDelayQueue(){
        HashMap<String, Object> map = new HashMap<String,Object>();
        //定义改队列中消息的生存时间 单位毫秒 生存时间最终对应业务的延迟执行时间
        map.put("x-message-ttl",60*10000);
        //x-dead-letter-exchange 属性用于声明死信转发到的交换机
        map.put("x-dead-letter-exchange","ordernum_delay_exchange");
        //x-dead-letter-routing-key 声明了这些死信在转发时携带的路由键名称
        map.put("x-dead-letter-routing-key","pay");
        return new Queue("ordernum_delay_queue",true,false,false,map);
    }

    /*
    死信交换机
     */
    @Bean
    public DirectExchange orderNumDelayExchange() {
        return new DirectExchange("ordernum_delay_exchange");
    }
    /*
    普通队列
     */
    @Bean
    public Queue orderNumQueue(){
        return new Queue("ordernum_queue");
    }
    /*
    绑定普通队列与死信交换机
     */
    @Bean
    public Binding bindOrdernumQueue(){
        return BindingBuilder.bind(orderNumQueue()).to(orderNumDelayExchange()).with("pay");
    }

}
